<?php 
/**
 * 邮件
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */

class EmailListPublicModel extends CommonModel {
	/// 模型名与数据表名不一致，需要指定
	protected 	$tableName = 'email_list'; 
	public 		$email_type = 0;

	public $email_type_array	=	array( 
									'Orders'=>1,
									'LoadContainer'=>2,
									'Instock'=>3,
									'SaleOrder'=>4,
									'Delivery'=>5,);

	 
	/**
	 * 订货单
	 *
	 * @param int $id
	 * @return array
	 */
	public function Orders($id){  
		if (C('order.email_order')!=1){ return  true; }
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$this->email_type	=	$this->email_type_array[__FUNCTION__];
		$model				=	M('orders');
		$list				=	$model->where('id='.$id)->field('id as object_id,'.$this->email_type.' as email_type, order_no as object_no,\''.$today.'\' as insert_time')->find();
		$this->setEmailList($id,$list); ///设置操作列表
	}
	
	
	/**
	 * 装柜单
	 *
	 * @param int $id
	 * @return array
	 */
	public function LoadContainer($id){  
		if (C('loadContainer.email_loadcontainer')!=1){ return  true; }
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$this->email_type	=	$this->email_type_array[__FUNCTION__];
		$model				=	M('LoadContainer');
		$list				=	$model->where('id='.$id.' and load_state<>2 ')->field('id as object_id,'.$this->email_type.' as email_type, load_container_no as object_no,\''.$today.'\' as insert_time')->find();
		$this->setEmailList($id,$list); ///设置操作列表
	}
	
	/**
	 * 装柜单
	 *
	 * @param int $id
	 * @return array
	 */
	public function Instock($id){  
		if (C('instock.email_instock')!=1){ return  true; }
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$this->email_type	=	$this->email_type_array[__FUNCTION__];
		$model				=	M('Instock');
		$list				=	$model->where('id='.$id)->field('id as object_id,'.$this->email_type.' as email_type, instock_no as object_no,\''.$today.'\' as insert_time')->find();
		$this->setEmailList($id,$list); ///设置操作列表
	}
	
	
	/**
	 * 装柜单
	 *
	 * @param int $id
	 * @return array
	 */
	public function SaleOrder($id){  
		if (C('sale.email_sale')!=1){ return  true; }
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$this->email_type	=	$this->email_type_array[__FUNCTION__];
		$model				=	M('SaleOrder');
		$list				=	$model->where('id='.$id)->field('id as object_id,'.$this->email_type.' as email_type, sale_order_no as object_no,\''.$today.'\' as insert_time')->find();
		$this->setEmailList($id,$list); ///设置操作列表
	}
	
	
	/**
	 * 装柜单
	 *
	 * @param int $id
	 * @return array
	 */
	public function Delivery($id){   
		if (C('delivery.email_delivery')!=1){ return  true; }
		$today 	= date("Y-m-d H:m:s");                           /// 20010310 
		$this->email_type	=	$this->email_type_array[__FUNCTION__];
		$model				=	M('Delivery');
		$list				=	$model->where('id='.$id)->field('id as object_id,'.$this->email_type.' as email_type, delivery_no as object_no,\''.$today.'\' as insert_time')->find();
		$this->setEmailList($id,$list); ///设置操作列表
	}
	 
	/**
	 * 设置操作列表
	 *
	 * @param int $id
	 * @param array $list
	 */
	public function setEmailList($id,$list){
		if (is_array($list)){
			$this->add($list);
		}else{
			$this->delete($id,$this->email_type); 
		} 
	}
	
	/**
	 * 删除
	 *
	 * @param int $id
	 * @param string $email_type
	 */
	public function delete($id,$email_type){ 
		if ($id>0 && $email_type>0){
			$this->where(' object_id='.$id.' and email_type='.$email_type)->setField('to_hide',2);	
		} 
	}
	
}